# -*- coding: utf-8 -*-

from openerp.osv import osv
from openerp.osv import fields
from openerp import SUPERUSER_ID


class wifidog_clients(osv.osv):
    _name = 'wifidog.clients'

    _columns = {
        'user_id': fields.many2one('res.users', u'用户'),
        'mac': fields.char(u'mac地址'),
        'token': fields.char(u'token'),
        'ip': fields.char(u'ip')
    }
    _sql_constraints = [
        ('token_uniq', 'unique (token)', u'token不能重复！'),
    ]


class hr_employee(osv.osv):
    _inherit = 'hr.employee'

    def create(self, cr, uid, vals, context=None):
        hr_employee_id = super(hr_employee, self).create(cr, uid, vals, context=context)
        hr_employee_objs = self.browse(cr, SUPERUSER_ID, hr_employee_id, context=context)
        hr_employee_objs.address_home_id.write({'mobile': vals.get('mobile_phone'), 'employee': True})

        return hr_employee_id

    def write(self, cr, uid, ids, vals, context=None):
        super(hr_employee, self).write(cr, uid, ids, vals, context=context)
        for hr_employee_objs in self.browse(cr, SUPERUSER_ID, ids, context=context):
            if vals.get('mobile_phone') and hr_employee_objs.address_home_id:
                hr_employee_objs.address_home_id.write({'mobile': vals.get('mobile_phone'), 'employee': True})
        return ids
